import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { type todo } from "@/type";

export const useToDoList = defineStore("toDoList", () => {
    const toDoList = ref([{ id: "001", matter: "吃饭", isFinish: false }]);
    const completedToDo = computed(() => {
        return toDoList.value.filter(function (item) {
            return item.isFinish
        })
    })
    const isAll = computed({
        get() {
            return completedToDo.value.length === toDoList.value.length
        },
        set(val: boolean) {
            toDoList.value.forEach(function (item) {
                item.isFinish = val
            })
        }
    })
    function addToDo(todo: todo) {
        toDoList.value.push(todo)
    }
    function deleteCurrent(id: string) {
        toDoList.value = toDoList.value.filter(function (item) {
            return item.id != id
        })
    }
    function checkItem(id: string) {
        toDoList.value.forEach(function (item) {
            if (item.id == id) {
                item.isFinish = !item.isFinish
                return
            }
        })
    }
    function deleteCompleted() {
        toDoList.value = toDoList.value.filter(function (item) {
            return !item.isFinish
        })
    }
    return {
        toDoList, completedToDo, isAll, addToDo, deleteCurrent, checkItem, deleteCompleted
    }
})